package com.nowadays.mobile.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

import com.nowadays.mobile.R;
import com.nowadays.mobile.utils.SettingInfo;

import net.xpece.android.support.preference.XpSeekBarPreferenceDialogFragment;

public class XpNumberSeekBarPreferenceDialogFragment extends XpSeekBarPreferenceDialogFragment {

  private String initSummary;

  public static XpNumberSeekBarPreferenceDialogFragment newInstance(String key) {
    XpNumberSeekBarPreferenceDialogFragment fragment = new XpNumberSeekBarPreferenceDialogFragment();
    Bundle b = new Bundle(1);
    b.putString("key", key);
    fragment.setArguments(b);
    return fragment;
  }

  @Override
  protected void onBindDialogView(View view) {
    super.onBindDialogView(view);

    SeekBar seekBar = getSeekBar(view);
    initSummary = seekBar.getProgress() + SettingInfo.STEP + seekBar.getContext().getString(R.string.minutes);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        android.support.v7.preference.DialogPreference preference = getPreference();
        String progressValue = progress + SettingInfo.STEP + preference.getContext().getString(R.string.minutes);
        preference.setSummary(progressValue);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {

      }
    });
  }

  @Override
  public void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (!positiveResult) {
      getPreference().setSummary(initSummary);
    }
  }
}
